<html>
<head><meta charset="utf-8"><title>Box&lt;T&gt; shallow copy aliasing/provenance · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box.3CT.3E.20shallow.20copy.20aliasing.2Fprovenance.html">Box&lt;T&gt; shallow copy aliasing/provenance</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="217513290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box%3CT%3E%20shallow%20copy%20aliasing/provenance/near/217513290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick12 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box.3CT.3E.20shallow.20copy.20aliasing.2Fprovenance.html#217513290">(Nov 21 2020 at 18:43)</a>:</h4>
<p>Is it ever sound to have more than  one Box&lt;T&gt; pointing to the same allocation?</p>
<p>I was watching a jonhoo stream about evmap and he said this: <a href="https://clips.twitch.tv/KitschyBlazingTaroDatSheffy">https://clips.twitch.tv/KitschyBlazingTaroDatSheffy</a><br>
He's talking about evmap and mentions making shallow Box&lt;T&gt; copies, when asked about aliasing and Unique&lt;T&gt; mentions it's fine as long as it's only accessed for reading and it's only dropped once. Is that correct under stacked borrows and llvm aliasing?</p>
<p>I think this is the code he's talking about: <a href="https://github.com/jonhoo/rust-evmap/blob/master/src/shallow_copy.rs#L65-L72">https://github.com/jonhoo/rust-evmap/blob/master/src/shallow_copy.rs#L65-L72</a></p>



<a name="217513508"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box%3CT%3E%20shallow%20copy%20aliasing/provenance/near/217513508" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box.3CT.3E.20shallow.20copy.20aliasing.2Fprovenance.html#217513508">(Nov 21 2020 at 18:48)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">mem</span>::<span class="n">ManuallyDrop</span>::<span class="n">new</span><span class="p">(</span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">std</span>::<span class="n">ptr</span>::<span class="n">read</span><span class="p">(</span><span class="o">&amp;</span><span class="n">a</span><span class="p">)</span><span class="w"> </span><span class="p">});</span><span class="w"></span>
<span class="w">    </span><span class="n">black_box</span><span class="p">(</span><span class="o">**</span><span class="n">b</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">black_box</span><span class="p">(</span><span class="o">*</span><span class="n">a</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">black_box</span><span class="p">(</span><span class="o">**</span><span class="n">b</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">black_box</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">(</span><span class="n">_</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>error: Undefined Behavior: no item granting read access to tag &lt;2946&gt; at alloc1411 found in borrow stack.
 --&gt; src/main.rs:6:15
  |
6 |     black_box(**b);
  |               ^^^ no item granting read access to tag &lt;2946&gt; at alloc1411 found in borrow stack.
  |
  = help: this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental
  = help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information

  = note: inside `main` at src/main.rs:6:15
</code></pre></div>



<a name="217513525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box%3CT%3E%20shallow%20copy%20aliasing/provenance/near/217513525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box.3CT.3E.20shallow.20copy.20aliasing.2Fprovenance.html#217513525">(Nov 21 2020 at 18:49)</a>:</h4>
<p>I don't think it is fine. I think the read of <code>*a</code> assumes exclusivity.</p>



<a name="217513727"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box%3CT%3E%20shallow%20copy%20aliasing/provenance/near/217513727" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box.3CT.3E.20shallow.20copy.20aliasing.2Fprovenance.html#217513727">(Nov 21 2020 at 18:54)</a>:</h4>
<p>it's more that <code>Box</code> is an exclusive ptr and even reads through aliases are disallowed when a ptr is exclusive</p>



<a name="217513735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box%3CT%3E%20shallow%20copy%20aliasing/provenance/near/217513735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box.3CT.3E.20shallow.20copy.20aliasing.2Fprovenance.html#217513735">(Nov 21 2020 at 18:54)</a>:</h4>
<p>(this grants extra optimization power because it means nobody else can even observe the current value stored behind the ptr)</p>



<a name="217523748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box%3CT%3E%20shallow%20copy%20aliasing/provenance/near/217523748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box.3CT.3E.20shallow.20copy.20aliasing.2Fprovenance.html#217523748">(Nov 21 2020 at 23:08)</a>:</h4>
<p>Side note, that code creates a box from a shared reference, which by itself might be UB even if the other box is forgotten.<br>
(because afaiu box acts similar to &amp;mut T, and you can't make that from a shared reference)</p>



<a name="217567489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box%3CT%3E%20shallow%20copy%20aliasing/provenance/near/217567489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nick12 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/Box.3CT.3E.20shallow.20copy.20aliasing.2Fprovenance.html#217567489">(Nov 22 2020 at 20:07)</a>:</h4>
<p>Opened an issue <a href="https://github.com/jonhoo/rust-evmap/issues/74">https://github.com/jonhoo/rust-evmap/issues/74</a></p>
<p>Also mentioned it in <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/145">https://github.com/rust-lang/unsafe-code-guidelines/issues/145</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>